<?php

class C_View 
{
	public $_ApplicationRoot = '';
	private $_params = array();
	private $_response = null;
	private $_helper = null;
	private $_template = '';
	public function __construct($response, $ApplicationRoot)
	{
		$this->_response = $response;
		$this->_ApplicationRoot = $ApplicationRoot;
		$this->_helper = new C_Helper($this);
	}
	public function initView($templatePath, $params)
	{
		$this->_template = $this->_ApplicationRoot.$templatePath;
		$this->_params = $params;
	}
	public function render()
	{	
	 	foreach($this->_params as $key=>$value)
	 	{
	 		$$key = $value;
	    }
	    require_once($this->_template);
	}
	public function partial($templatePath, $params = array())
	{	
		$this->initView($templatePath, $params);
		$this->render();
	}
	public function action($module,  $action,  $method)
	{
		$this->_response->startAction($module,  $action,  $method);
	}
	public function getUrl($module,  $action,  $method, $params = array() , $query = array())
	{
		return $this->_response->getUrl($module,  $action,  $method,  $params,$query);
	}
	public function cutString($string,$length) 
	{
       preg_match_all("/[\x01-\x7f]|[\xc2-\xdf][\x80-\xbf]|\xe0[\xa0-\xbf][\x80-\xbf]|[\xe1-\xef][\x80-\xbf][\x80-\xbf]|\xf0[\x90-\xbf][\x80-\xbf][\x80-\xbf]|[\xf1-\xf7][\x80-\xbf][\x80-\xbf][\x80-\xbf]/", $string, $info);  
        for($i=0; $i<count($info[0]); $i++) 
        {
                $str .= $info[0][$i];
                $j = ord($info[0][$i]) > 127 ? $j + 2 : $j + 1;
                if ($j > $length - 3) {
                        return $str.'...';
                }
        }
        return join('', $info[0]);
	}
	public function cutHtml($string, $number)
	{
		//带空格
		//preg_match_all('/\>([^>]+?)(?=\<)/',$string, $match);
		//不带空格
		preg_match_all('/(?:\>|&nbsp;)([^>]+?)(?=(?:\<|&nbsp;))/',$string, $match);
		$cutHtml = implode($match[1]);
		return $this->cutString($cutHtml, $number);
	}
	public function fcutHtml($string, $number)
	{
		return $this->cutString(strip_tags($string), $number);
	}
	public function encrypt($str)
	{
		return T_SimpleEncrypt::passport_encrypt($str);
	}
}
?>